﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZX.HIS.IDAL;
using ZX.HIS.DBUtility;
using System.Collections.Generic;//Please add references
namespace ZX.HIS.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:Hospital
	/// </summary>
	public partial class Hospital:IHospital
	{
		public Hospital()
		{}
       
        #region Depart自写方法

        /// <summary>
        /// 人员登陆
        /// </summary>
        /// <returns></returns>
        public List<ZX.HIS.Model.UserInfo> loginHospital(string UserName, string UserPwd)
        {
            string sql = "SELECT * FROM tbUserInfo  WHERE UserName='" + UserName + "' AND UserPwd='" + UserPwd + "' ";

            return SqlDBhelper.GetList<ZX.HIS.Model.UserInfo>(sql);
        }

        /// <summary>
        /// 诊所注册
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int registHospital(ZX.HIS.Model.Hospital model)
        {
            StringBuilder strsql = new StringBuilder();
            strsql.AppendLine("INSERT INTO tbHospital(");
            strsql.AppendLine("HospitalUserName,HospitalPhone,HospitalName,HospitalAddre,HospitalNo,HospitalTime)");
            strsql.AppendLine("  VALUES(");
            strsql.AppendLine("@HospitalUserName,@HospitalPhone,@HospitalName,@HospitalAddre,@HospitalNo,GETDATE() )");
            strsql.AppendLine(";select @@IDENTITY");
            SqlParameter[] parameters = { 
                                       
                new SqlParameter("@HospitalUserName", SqlDbType.VarChar),
                new SqlParameter("@HospitalPhone", SqlDbType.VarChar),
                new SqlParameter("@HospitalName", SqlDbType.VarChar),
                new SqlParameter("@HospitalAddre", SqlDbType.VarChar),
                new SqlParameter("@HospitalNo", SqlDbType.VarChar)
               
                                        } ;
            parameters[0].Value = model.HospitalUserName;
            parameters[1].Value = model.HospitalPhone;
            parameters[2].Value = model.HospitalName;
            parameters[3].Value = model.HospitalAddre;
            parameters[4].Value = model.HospitalNo;

           
            return Convert.ToInt32(SqlDBhelper.ExecuteScalar(strsql.ToString(),parameters));
        }
        /// <summary>
        /// 添加人员表
        /// </summary>
        /// <returns></returns>
        public int addUserInfo(int HospitalId,string UserName, string UserPwd, string UserChineseName)
        {
            StringBuilder strsql = new StringBuilder();
            strsql.AppendLine("INSERT INTO tbUserInfo (");
            strsql.AppendLine(" HospitalId,UserName,UserPwd,UserChineseName,UserState)");
            strsql.AppendLine("  VALUES(");
            strsql.AppendLine("@HospitalId,@UserName,@UserPwd,@UserChineseName,1)");
          
            SqlParameter[] parameters = { 
                new SqlParameter("@HospitalId", SqlDbType.Int),          
                new SqlParameter("@UserName", SqlDbType.VarChar),
                new SqlParameter("@UserPwd", SqlDbType.VarChar),
                new SqlParameter("@UserChineseName", SqlDbType.VarChar)
       
                                        };
            parameters[0].Value = HospitalId;
            parameters[1].Value = UserName;
            parameters[2].Value = UserPwd;
            parameters[3].Value = UserChineseName;
          
            return  SqlDBhelper.ExecuteNonQuery(strsql.ToString(), parameters);
        }
        /// <summary>
        /// 修改诊所信息
        /// </summary>
        /// <param name="HospitalName"></param>
        /// <param name="HospitalAddre"></param>
        /// <param name="HospitalId"></param>
        /// <returns></returns>
        public int updaHospital(string HospitalName,string HospitalAddre,int HospitalId)
        { 
       
             StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbHospital set HospitalName=@HospitalName,HospitalAddre=@HospitalAddre");
            strSql.Append(" where HospitalId=@HospitalId");
           SqlParameter[] parameters = {
                   new SqlParameter("@HospitalName", SqlDbType.VarChar),
                    new SqlParameter("@HospitalAddre", SqlDbType.VarChar)	,
		               new SqlParameter("@HospitalId", SqlDbType.Int)		
                           };
           parameters[0].Value = HospitalName;
           parameters[1].Value = HospitalAddre;
           parameters[2].Value = HospitalId;
           return SqlDBhelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 诊所信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public List<ZX.HIS.Model.Hospital> model(int UserId)
        {
            string sql = "select * from  tbHospital where HospitalId in (select  HospitalId  from  tbUserInfo  where UserId='"+UserId+"' )";
            return SqlDBhelper.GetList<ZX.HIS.Model.Hospital>(sql);
        }
        #endregion
        #region  BasicMethod

        ///// <summary>
        ///// 得到最大ID
        ///// </summary>
        //public int GetMaxId()
        //{
        //return DbHelperSQL.GetMaxID("HospitalId", "tbHospital"); 
        //}

        ///// <summary>
        ///// 是否存在该记录
        ///// </summary>
        //public bool Exists(string HospitalPhone, int HospitalId)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select count(1) from tbHospital");
        //    strSql.Append(" where HospitalPhone=@HospitalPhone and HospitalId=@HospitalId ");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalPhone", SqlDbType.VarChar,12),
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4)			};
        //    parameters[0].Value = HospitalPhone;
        //    parameters[1].Value = HospitalId;

        //    return DbHelperSQL.Exists(strSql.ToString(), parameters);
        //}


        ///// <summary>
        ///// 增加一条数据
        ///// </summary>
        //public int Add(ZX.HIS.Model.Hospital model)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("insert into tbHospital(");
        //    strSql.Append("HospitalUserName,HospitalPhone,HospitalName,HospitalAddre,HospitalNo,HospitalTime)");
        //    strSql.Append(" values (");
        //    strSql.Append("@HospitalUserName,@HospitalPhone,@HospitalName,@HospitalAddre,@HospitalNo,@HospitalTime)");
        //    strSql.Append(";select @@IDENTITY");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalUserName", SqlDbType.VarChar,20),
        //            new SqlParameter("@HospitalPhone", SqlDbType.VarChar,12),
        //            new SqlParameter("@HospitalName", SqlDbType.VarChar,30),
        //            new SqlParameter("@HospitalAddre", SqlDbType.VarChar,100),
        //            new SqlParameter("@HospitalNo", SqlDbType.VarChar,20),
        //            new SqlParameter("@HospitalTime", SqlDbType.date,3)};
        //    parameters[0].Value = model.HospitalUserName;
        //    parameters[1].Value = model.HospitalPhone;
        //    parameters[2].Value = model.HospitalName;
        //    parameters[3].Value = model.HospitalAddre;
        //    parameters[4].Value = model.HospitalNo;
        //    parameters[5].Value = model.HospitalTime;

        //    object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 更新一条数据
        ///// </summary>
        //public bool Update(ZX.HIS.Model.Hospital model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("update tbHospital set ");
        //    strSql.Append("HospitalUserName=@HospitalUserName,");
        //    strSql.Append("HospitalName=@HospitalName,");
        //    strSql.Append("HospitalAddre=@HospitalAddre,");
        //    strSql.Append("HospitalNo=@HospitalNo,");
        //    strSql.Append("HospitalTime=@HospitalTime");
        //    strSql.Append(" where HospitalId=@HospitalId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalUserName", SqlDbType.VarChar,20),
        //            new SqlParameter("@HospitalName", SqlDbType.VarChar,30),
        //            new SqlParameter("@HospitalAddre", SqlDbType.VarChar,100),
        //            new SqlParameter("@HospitalNo", SqlDbType.VarChar,20),
        //            new SqlParameter("@HospitalTime", SqlDbType.date,3),
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@HospitalPhone", SqlDbType.VarChar,12)};
        //    parameters[0].Value = model.HospitalUserName;
        //    parameters[1].Value = model.HospitalName;
        //    parameters[2].Value = model.HospitalAddre;
        //    parameters[3].Value = model.HospitalNo;
        //    parameters[4].Value = model.HospitalTime;
        //    parameters[5].Value = model.HospitalId;
        //    parameters[6].Value = model.HospitalPhone;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        ///// <summary>
        ///// 删除一条数据
        ///// </summary>
        //public bool Delete(int HospitalId)
        //{
			
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbHospital ");
        //    strSql.Append(" where HospitalId=@HospitalId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = HospitalId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
        ///// <summary>
        ///// 删除一条数据
        ///// </summary>
        //public bool Delete(string HospitalPhone,int HospitalId)
        //{
			
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbHospital ");
        //    strSql.Append(" where HospitalPhone=@HospitalPhone and HospitalId=@HospitalId ");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalPhone", SqlDbType.VarChar,12),
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4)			};
        //    parameters[0].Value = HospitalPhone;
        //    parameters[1].Value = HospitalId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
        ///// <summary>
        ///// 批量删除数据
        ///// </summary>
        //public bool DeleteList(string HospitalIdlist )
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbHospital ");
        //    strSql.Append(" where HospitalId in ("+HospitalIdlist + ")  ");
        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.Hospital GetModel(int HospitalId)
        //{
			
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select  top 1 HospitalId,HospitalUserName,HospitalPhone,HospitalName,HospitalAddre,HospitalNo,HospitalTime from tbHospital ");
        //    strSql.Append(" where HospitalId=@HospitalId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = HospitalId;

        //    ZX.HIS.Model.Hospital model=new ZX.HIS.Model.Hospital();
        //    DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
        //    if(ds.Tables[0].Rows.Count>0)
        //    {
        //        return DataRowToModel(ds.Tables[0].Rows[0]);
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.Hospital DataRowToModel(DataRow row)
        //{
        //    ZX.HIS.Model.Hospital model=new ZX.HIS.Model.Hospital();
        //    if (row != null)
        //    {
        //        if(row["HospitalId"]!=null && row["HospitalId"].ToString()!="")
        //        {
        //            model.HospitalId=int.Parse(row["HospitalId"].ToString());
        //        }
        //        if(row["HospitalUserName"]!=null)
        //        {
        //            model.HospitalUserName=row["HospitalUserName"].ToString();
        //        }
        //        if(row["HospitalPhone"]!=null)
        //        {
        //            model.HospitalPhone=row["HospitalPhone"].ToString();
        //        }
        //        if(row["HospitalName"]!=null)
        //        {
        //            model.HospitalName=row["HospitalName"].ToString();
        //        }
        //        if(row["HospitalAddre"]!=null)
        //        {
        //            model.HospitalAddre=row["HospitalAddre"].ToString();
        //        }
        //        if(row["HospitalNo"]!=null)
        //        {
        //            model.HospitalNo=row["HospitalNo"].ToString();
        //        }
        //        if(row["HospitalTime"]!=null && row["HospitalTime"].ToString()!="")
        //        {
        //            model.HospitalTime=DateTime.Parse(row["HospitalTime"].ToString());
        //        }
        //    }
        //    return model;
        //}

        ///// <summary>
        ///// 获得数据列表
        ///// </summary>
        //public DataSet GetList(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select HospitalId,HospitalUserName,HospitalPhone,HospitalName,HospitalAddre,HospitalNo,HospitalTime ");
        //    strSql.Append(" FROM tbHospital ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获得前几行数据
        ///// </summary>
        //public DataSet GetList(int Top,string strWhere,string filedOrder)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select ");
        //    if(Top>0)
        //    {
        //        strSql.Append(" top "+Top.ToString());
        //    }
        //    strSql.Append(" HospitalId,HospitalUserName,HospitalPhone,HospitalName,HospitalAddre,HospitalNo,HospitalTime ");
        //    strSql.Append(" FROM tbHospital ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    strSql.Append(" order by " + filedOrder);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获取记录总数
        ///// </summary>
        //public int GetRecordCount(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) FROM tbHospital ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    object obj = DbHelperSQL.GetSingle(strSql.ToString());
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("SELECT * FROM ( ");
        //    strSql.Append(" SELECT ROW_NUMBER() OVER (");
        //    if (!string.IsNullOrEmpty(orderby.Trim()))
        //    {
        //        strSql.Append("order by T." + orderby );
        //    }
        //    else
        //    {
        //        strSql.Append("order by T.HospitalId desc");
        //    }
        //    strSql.Append(")AS Row, T.*  from tbHospital T ");
        //    if (!string.IsNullOrEmpty(strWhere.Trim()))
        //    {
        //        strSql.Append(" WHERE " + strWhere);
        //    }
        //    strSql.Append(" ) TT");
        //    strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///*
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@tblName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@fldName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@PageSize", SqlDbType.Int),
        //            new SqlParameter("@PageIndex", SqlDbType.Int),
        //            new SqlParameter("@IsReCount", SqlDbType.Bit),
        //            new SqlParameter("@OrderType", SqlDbType.Bit),
        //            new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
        //            };
        //    parameters[0].Value = "tbHospital";
        //    parameters[1].Value = "HospitalId";
        //    parameters[2].Value = PageSize;
        //    parameters[3].Value = PageIndex;
        //    parameters[4].Value = 0;
        //    parameters[5].Value = 0;
        //    parameters[6].Value = strWhere;	
        //    return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        //}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

